Page({
  data: {
    passportInfo: {},
  },
  onLoad({ id, encryptedData }) {
    if (id) {
      this.initData(id)
    }
    if (encryptedData) {
      this.getPassportInfo(encryptedData)
    }

  },
  // 获取详情
  async initData(id) {
    const passportInfo = await wx.http.get('/visitor/' + id)
    // console.log(passportInfo);
    this.setData({ passportInfo })
  },
  // 下载到本地
  async onLoadFile() {
    const { path } = await wx.getImageInfo({
      src: this.data.passportInfo.url,
    })
    //  console.log(path);
    await wx.saveImageToPhotosAlbum({
      filePath: path,
    })
    wx.utils.toast('保存本地成功')
  },
  // 分享
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.passportInfo.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  // 分享页面获取详情
  async getPassportInfo(encryptedData) {
    const passportInfo = await wx.http.get('/visitor/share/' + encryptedData)
    // console.log(res);
    this.setData({ passportInfo })
  }
})
